package com.testDemo.jdk8.uils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.testDemo.lang.vo.TestVo1;
import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @author: zouren
 * @date: 2021/12/9
 * @description:
 */
public class ListUtilsTest {
    @Test
    public void add(){
        List list = new ArrayList();
        list.add("A");
        list.add("B");
        list.add("C");
        List list2 = new ArrayList();
        list2.add("A1");
        list2.add("B2");
        list2.add("C3");
        list.addAll(0,list2);
        System.out.println(list);
    }
    private TestVo1 create(int i ){
        TestVo1 testVo1 = new TestVo1();
        testVo1.setMsg("msg"+i);
        if((i&2)==0){
            testVo1.setTimestamp(new Date());
        }else {
            testVo1.setTimestamp(DateUtil.offsetDay(new Date(),i-10));
        }
        return testVo1;

    }
    @Test
    public void sort(){
        List<TestVo1> list = new ArrayList();
        for (int i = 0; i < 11; i++) {
            TestVo1 aa =  create(i);
            list.add(aa);
        }
        for (int i = 0; i < list.size(); i++) {
            TestVo1 a = list.get(i);
            System.out.println(a.getMsg()+"==="+DateUtil.format(a.getTimestamp(),"yyyy-MM-dd HH:mm:ss"));
        }
        System.out.println("=====================");
        list.sort(Comparator.comparing(TestVo1::getTimestamp));
        for (int i = 0; i < list.size(); i++) {
            TestVo1 a = list.get(i);
            System.out.println(a.getMsg()+"==="+DateUtil.format(a.getTimestamp(),"yyyy-MM-dd HH:mm:ss"));
        }
    }
}
